package com.lpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpit.mapper.ArticleMapper;
import com.lpit.vo.ArticleVo;
import com.lpit.result.ResponseResult;
import com.lpit.dto.UserHomeDto;
import com.lpit.pojo.Fans;
import com.lpit.pojo.User;
import com.lpit.mapper.BrowsingHistoryMapper;
import com.lpit.mapper.FansMapper;
import com.lpit.mapper.UserMapper;
import com.lpit.vo.UserVo;
import com.lpit.service.UserService;
import com.lpit.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    @Autowired
    private FansMapper fansMapper;
    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 加载用户主页个人资料
     *
     * @param userHomeDto
     * @return
     */
    public ResponseResult<UserVo> getUserHome(UserHomeDto userHomeDto) {
        if (null == userHomeDto) {
            userHomeDto = new UserHomeDto();
        }
        /*判断需要的属性非空*/
        if (null == userHomeDto.getId()) {
            return new ResponseResult<>(500, null, "id不能为空");
        }
        /*获取用户资料*/
        User user = userMapper.selectById(userHomeDto.getId());
        /*获取有多少点赞数*/
        Integer integer = browsingHistoryMapper.countUpvote(userHomeDto.getId());
        /*获取粉丝数量*/
        LambdaQueryWrapper<Fans> fansLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fansLambdaQueryWrapper.eq(Tool.isNotEmpty(userHomeDto.getId().toString()), Fans::getUserId, userHomeDto.getId());
        List<Fans> fans = fansMapper.selectList(fansLambdaQueryWrapper);
        /*获取关注数量*/
        LambdaQueryWrapper<Fans> concernQueryWrapper = new LambdaQueryWrapper<>();
        concernQueryWrapper.eq(Tool.isNotEmpty(userHomeDto.getId().toString()), Fans::getFansId, userHomeDto.getId());
        List<Fans> concern = fansMapper.selectList(concernQueryWrapper);
        /*将查出来的数据封装*/
        UserVo userVo = new UserVo();
        userVo.setUser(user);
        userVo.setConcern(concern.size());
        userVo.setFans(fans.size());
        userVo.setTops(integer);
        /*返回数据*/
        return new ResponseResult<>(userVo);
    }

    /**
     * 加载个人主页作品
     *
     * @param userHomeDto
     * @return
     */
    @Override
    public ResponseResult<List<ArticleVo>> getUserArticle(UserHomeDto userHomeDto) {
        /*判断需要的属性非空*/
        if (null == userHomeDto) {
            userHomeDto = new UserHomeDto();
        }
        if (null == userHomeDto.getNewestTime()) {
            userHomeDto.setNewestTime(new Date());
        }
        if (null == userHomeDto.getSize()) {
            userHomeDto.setSize(10);
        }
        /*判断传过来的id不为空*/
        if (null == userHomeDto.getId()) {
            return new ResponseResult<>(500, null, "id不能为空");
        }
        /*创建分页*/
        IPage<ArticleVo> articleVoIPage = new Page<>(0, userHomeDto.getSize());
        /*查询用户的文章*/
        List<ArticleVo> articleVos = articleMapper.selectArticlePageViceAndCommentsByUserId(articleVoIPage, userHomeDto.getId());
        /*返回结果*/
        return new ResponseResult<>(articleVos);
    }

    /**
     * 获取用户文章最热前十条
     */
    @Override
    public ResponseResult<List<ArticleVo>> getUserArticleSort(UserHomeDto userHomeDto) {
        /*判断需要的属性非空*/
        if (null == userHomeDto) {
            userHomeDto = new UserHomeDto();
        }
        /*判断传过来的id不为空*/
        if (null == userHomeDto.getId()) {
            return new ResponseResult<>(500, null, "id不能为空");
        }
        /*查询*/
        List<ArticleVo> articleVos = articleMapper.selectArticlePageViewSortByUserId(userHomeDto.getId());
        /*返回结果*/
        return new ResponseResult<>(articleVos);
    }


}
